//
//  Date+Extension.swift
//  Test
//
//  Created by ZWJY on 2021/8/21.
//

import Foundation
import UIKit

public extension Date {

    /// 获取当前时间戳-10位-秒
    static var currentSecondTimeStamp: String {
        let date = Date.init()
        let interval = Int(TimeInterval(date.timeIntervalSince1970))
        return "\(interval)"
    }

    /// 获取当前时间戳-13位-毫秒
    static var currentmilliTimeStamp: String {
        let date = Date.init()
        let interval = Int(TimeInterval(date.timeIntervalSince1970) * 1000)
        return "\(interval)"
    }

    /// 获取当前时区的Date
    static var currentDate: Date {
        let nowDate = Date.init()
        let zone = NSTimeZone.system
        let interval = zone.secondsFromGMT(for: nowDate)
        let localeDate = nowDate.addingTimeInterval(TimeInterval(interval))
        return localeDate
    }

    ///  格式化当前Date 成 String
    static var currentDateString: String {
        let date = self.currentDate
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY-MM-dd"
        let string = dateFormatter.string(from: date)
        return string
    }

    /// 获取date 年份
    static var year: Int{
        return Calendar.current.component(.year, from: currentDate)
    }

    /// 获取月份
    static var month: Int{
        return Calendar.current.component(.month, from: currentDate)
    }

    /// 获取日
    static var day: Int{
        return Calendar.current.component(.day, from: currentDate)
    }

    /// 获取小时
    static var hour: Int{
        return Calendar.current.component(.hour, from: currentDate)
    }

    /// 获取分钟
    static var minute: Int{
        return Calendar.current.component(.minute, from: currentDate)
    }

    /// 获取秒数
    static var second: Int{
        return Calendar.current.component(.second, from: currentDate)
    }

    /// 获取毫秒
    static var nanosecond: Int{
        return Calendar.current.component(.nanosecond, from: currentDate)
    }

    /// 时间戳string转date
    static func timestampToFormatterDate(_ timeStamp: String) -> Date{
        guard timeStamp.isEmpty, let time = TimeInterval(timeStamp) else { return Date() }
        let value = timeStamp.count == 10 ? time : time / 1000
        let date = Date.init(timeIntervalSince1970: TimeInterval(value))
        return date
    }

    ///时间戳string转时间格式string
    /// yyyy-MM-dd HH:mm:ss
    static func timestampToFormatterDateString(_ timeStamp: String, _ format: String) -> String{
        let date = timestampToFormatterDate(timeStamp)
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        let dateString = dateFormatter.string(from: date)
        return dateString
    }
    
    /// 年月日 xx-xx-xx -> date
    static func dayStringToFormatterDate(_ day: String) -> Date {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        return dateFormatter.date(from: day) ?? currentDate
    }
    
    static func dateToString(_ date: Date) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY-MM-dd"
        let string = dateFormatter.string(from: date)
        return string
    }
    
    ///时间格式string转时间戳string
    static func dateStringToFormatterTimestampString (_ dateString: String, _ format: String) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        let tempDate = dateFormatter.date(from: dateString)
        return String(tempDate!.timeIntervalSince1970) // 13位 * 1000
    }

    /// 判断timeStamp,是星期几
    static func weekdayStringFromDate(_ timeStamp: String) -> String {
        let date = timestampToFormatterDate(timeStamp)
        let weekdays:NSArray = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
        var calendar = Calendar.init(identifier: .gregorian)
        let timeZone = TimeZone.init(identifier: "Asia/Shanghai")
        calendar.timeZone = timeZone!
        let theComponents = calendar.dateComponents([.weekday], from: date)
        return weekdays.object(at: (theComponents.weekday! - 1)) as! String
    }

    // MARK: 4.1、取得与当前时间的间隔差
    /// 取得与当前时间的间隔差
    /// - Returns: 时间差
    func callTimeAfterNow() -> String {
        let timeInterval = Date().timeIntervalSince(Date.currentDate)
        if timeInterval < 0 {
            return "刚刚"
        }
        let interval = fabs(timeInterval)
        let i60 = interval / 60
        let i3600 = interval / 3600
        let i86400 = interval / 86400
        let i2592000 = interval / 2592000
        let i31104000 = interval / 31104000

        var time:String!
        if i3600 < 1 {
            let s = NSNumber(value: i60 as Double).intValue
            if s == 0 {
                time = "刚刚"
            } else {
                time = "\(s)分钟前"
            }
        } else if i86400 < 1 {
            let s = NSNumber(value: i3600 as Double).intValue
            time = "\(s)小时前"
        } else if i2592000 < 1 {
            let s = NSNumber(value: i86400 as Double).intValue
            time = "\(s)天前"
        } else if i31104000 < 1 {
            let s = NSNumber(value: i2592000 as Double).intValue
            time = "\(s)个月前"
        } else {
            let s = NSNumber(value: i31104000 as Double).intValue
            time = "\(s)年前"
        }
        return time
    }
}
